package com.yww.player

import android.Manifest
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.tbruyelle.rxpermissions2.Permission
import com.tbruyelle.rxpermissions2.RxPermissions
import com.yww.player.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private val TAG = "MainActivity"
    private var isHavePermissions = false

    @RequiresApi(Build.VERSION_CODES.Q)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding: ActivityMainBinding =
            DataBindingUtil.setContentView(this, R.layout.activity_main)
        binding.apply {
            btnVideoList.setOnClickListener {
                startActivity(Intent(this@MainActivity, VideoListActivity::class.java))
//                val intent = Intent(this@MainActivity, PlayVideoActivity::class.java)
//                val list = assets.list("mp4")
//                intent.putExtra("url", File("/sdcard/map4/test.mp4").absolutePath)
//                startActivity(intent)
            }
        }
        requestPermissions()
        if (permissionResult.size != 0) {
            Toast.makeText(baseContext, "请在设置中开启权限否则无法使用!", Toast.LENGTH_LONG)
                .show()
        }
    }

    /**
     * 权限组
     */
    @RequiresApi(Build.VERSION_CODES.Q)
    val permissionsGroup = arrayOf(
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
    )
    val permissionResult = mutableListOf<Permission>()

    @RequiresApi(Build.VERSION_CODES.Q)
    @SuppressLint("CheckResult")
    fun requestPermissions() {
        var rxPermissions = RxPermissions(this)
        rxPermissions.requestEach(*permissionsGroup)
            .subscribe {
                isHavePermissions = it.granted
                Log.d(TAG, "requestPermissions: $isHavePermissions")
                Log.d(TAG, "requestPermissions: ${it.name}")
                if (!it.granted) {
                    permissionResult.add(it)
                }
            }
    }

}